package com.bitdf.txing.group.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bitdf.txing.common.utils.PageUtils;
import com.bitdf.txing.common.utils.R;
import com.bitdf.txing.common.vo.UserInfoVo;
import com.bitdf.txing.group.dao.GroupMemberDao;
import com.bitdf.txing.group.entity.GroupMemberEntity;
import com.bitdf.txing.group.feign.EsFeignService;
import com.bitdf.txing.group.service.GroupMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("groupMemberService")
public class GroupMemberServiceImpl extends ServiceImpl<GroupMemberDao, GroupMemberEntity> implements GroupMemberService {

    @Autowired
    EsFeignService esFeignService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {

//        QueryWrapper<GroupMemberEntity> wrapper = new QueryWrapper<>();
//        wrapper.eq(params.get("groupId")!=null&& !params.get("groupId").equals(""), "group_id", params.get("groupId"));
//        IPage<GroupMemberEntity> page = this.page(
//                new Query<GroupMemberEntity>().getPage(params),
//                wrapper
//        );
//
//        return new PageUtils(page);
        return null;
    }

    /**
     * 移除项目成员
     * @param userId
     * @param groupId
     * @return
     */
    @Override
    @Transactional
    public boolean deleteByUserIdAndGroupId(Long userId, Long groupId) {
        //删除数据库数据
        boolean remove = this.remove(new QueryWrapper<GroupMemberEntity>().eq("user_id", userId).and((queryWrapper) -> {
            queryWrapper.eq("group_id", groupId);
        }));
        //更改es中对应组团记录的人数字段值
        R r = esFeignService.countIncreOrDcre(groupId, -1, "currentPersonCount");
        return remove && r.getCode()==0;
    }

    /**
     * 通过组团id获取所有成员id集合
     * @param groupId
     * @return
     */
    @Override
    public List<Long> getMemberIdsByGroupId(Long groupId) {
        List<GroupMemberEntity> list = this.list(new QueryWrapper<GroupMemberEntity>().eq("group_id", groupId));
        List<Long> collect = list.stream().map((entity) -> {
            return entity.getUserId();
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 获取某用户的群及用户信息
     * @param userId
     * @return
     */
    @Override
    public Map<Long, List<Long>> getGroupAndUserIdsByUserId(Long userId) {
        //获取该用户组团id集合
        List<GroupMemberEntity> list = this.list(new QueryWrapper<GroupMemberEntity>().eq("user_id", userId));
        List<Long> groupIdList = list.stream().map(entity -> entity.getGroupId()).collect(Collectors.toList());
        //获取各组团的用户ids
        Map<Long, List<Long>> listMap = groupIdList.stream().collect(Collectors.toMap(id -> {
            return id;
        }, id -> {
            List<GroupMemberEntity> list1 = this.list(new QueryWrapper<GroupMemberEntity>().eq("group_id", id));
            return list1.stream().map((item) -> {
                return item.getUserId();
            }).collect(Collectors.toList());
        }));
        return listMap;
    }

    /**
     * 添加活动新成员
     * @param groupId
     * @param userId
     * @return
     */
    @Override
    @Transactional
    public boolean addMember(Long groupId, Long userId) {
        //判定当前用户是否为该组团的负责人，如果不是，则无权限完成此操作
        UserInfoVo userInfoVo = (UserInfoVo) StpUtil.getSession().get("user");
        GroupMemberEntity one = this.getOne(new QueryWrapper<GroupMemberEntity>().eq("user_id", userInfoVo.getId()).eq("group_id", groupId));
        if(one==null || one.getLeaderStatus()!=1){
            return false;
        }
        //保存到数据库
        GroupMemberEntity entity = new GroupMemberEntity(groupId, userId, (byte)0);
        boolean save = this.save(entity);
        //远程更新es中组团数据的成员数字段
        esFeignService.countIncreOrDcre(groupId, 1, "currentPersonCount");
        return true;
    }
}